package com.douguo.recipe.network;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.lang.annotation.Target;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.util.List;

import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIUtils;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.CoreConnectionPNames;

import android.content.ContentValues;
import android.net.Uri;
import android.text.style.ReplacementSpan;
import android.util.Log;

public class DouguoConnection {

	private static final String TAG = "DouguoConnection";

	private String mUrl;
	private List<NameValuePair> mNvps;
	private OnParseListener mOnParseListener;
	private int mMethod;
	private DefaultHttpClient mClient;
	public static final int GET_METHOD = 0;
	public static final int POST_METHOD = 1;
	private static final int CONNECT_TIMEOUT = 10000;
	private static final int SO_TIMEOUT = 10000;

	// public DouguoConnection(){
	//
	// }
	public DouguoConnection(String url, List<NameValuePair> nvps,
			OnParseListener litsn, int method) {
		mUrl = url;
		mNvps = nvps;
		mOnParseListener = litsn;
		mMethod = method;
	}

	public Object Connect() {
		mClient = new DefaultHttpClient();
		mClient.getParams().setParameter(
				CoreConnectionPNames.CONNECTION_TIMEOUT, CONNECT_TIMEOUT);
		mClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT,
				SO_TIMEOUT);

		// if (Util.networkInfoString.equals(Util.NETWORK_CMWAP)
		// || Util.networkInfoString.equals(Util.NETWORK_UIWAP)) {
		// HttpHost proxy = new HttpHost(Util.PROXY, 80, "http");
		// mClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,
		// proxy);
		// }

		if (Util.proxy != null && !Util.proxy.equals("") && Util.port != null
				&& !Util.port.equals("")) {
			Log.d(TAG, "proxy:"+Util.proxy+" port:"+Util.port);
			HttpHost proxy = new HttpHost(Util.proxy, Integer.valueOf(Util.port), "http");
			mClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,
			proxy);
		}

		if (mMethod == GET_METHOD) {

			StringBuilder sb = new StringBuilder();
			if (mNvps != null) {
				if (mNvps.size() > 0)
					sb.append("?");
				for (NameValuePair nvp : mNvps) {
					sb.append(nvp.getName() + "=" + nvp.getValue() + "&");
				}
			}
			String desUrl = mUrl + sb;
			desUrl = desUrl.replace(" ", "%20");
			Log.d(TAG, desUrl);
			HttpGet hget = new HttpGet(desUrl);
			// URIUtils.createURI("http", mUrl, -1, "",
			// URLEncodedUtils.format(mNvps, "utf-8"), null);
			try {
				HttpResponse rsp = mClient.execute(hget);
				if (rsp.getStatusLine().getStatusCode() == 200) {// status ok
					
//					 String result =
//					 inputStreamToString(rsp.getEntity().getContent());
					return mOnParseListener.onParsing(rsp.getEntity()
							.getContent());
				} else {
					Log.d(TAG, "code error");
					return null;
				}
			} catch (ClientProtocolException e) {
				Log.d(TAG, "client error");
				e.printStackTrace();
			} catch (IOException e) {
				Log.d(TAG, "io error");
				e.printStackTrace();
			} catch (Exception e){
				e.printStackTrace();
			}
		} else if (mMethod == POST_METHOD) {
			HttpPost hpost = new HttpPost(mUrl);
			try {
				hpost.setEntity(new UrlEncodedFormEntity(mNvps, "utf-8"));
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				HttpResponse rsp = mClient.execute(hpost);
				if (rsp.getStatusLine().getStatusCode() == 200) {
					// String result =
					// inputStreamToString(rsp.getEntity().getContent());
					return mOnParseListener.onParsing(rsp.getEntity()
							.getContent());
				}
			} catch (ClientProtocolException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return null;
	}

}
